/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* MpmWorkingCalendarService
* 工作日历
*
* 修改纪录
* 2021-07-05 版本：1.0 wudaliang 创建。
* @version 版本：1.0
* @author 作者：wudaliang
* 创建日期：2021-07-05
*/
package com.topscomm.mpm.service.impl;

import cn.hutool.core.date.DateUtil;
import com.topscomm.cbo.CboSystemConst.CboMultipleOvertimeEnum;
import com.topscomm.cbo.CboSystemConst.CboWorkingCalendarCategoryEnum;
import com.topscomm.cbo.pojo.CboWorkingCalendarEntity;
import com.topscomm.mpm.pojo.MpmWorkingCalendarEntity;
import com.topscomm.pub.util.ConvertUtil;
import java.math.BigDecimal;
import java.util.*;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmWorkingCalendarService;
import com.topscomm.mpm.service.impl.auto.MpmWorkingCalendarServiceAuto;

@Service(value = "mpmWorkingCalendarService")
@Scope(value = "singleton")
public class MpmWorkingCalendarService extends MpmWorkingCalendarServiceAuto implements IMpmWorkingCalendarService {

    public void insertCalendarBatch(Map paraMap) {
        String periodmonth=ConvertUtil.convertToString(paraMap.get("qry_periodmonth"));
        String calendartype=ConvertUtil.convertToString(paraMap.get("qry_calendartype"));
        //获取已经有的数据 不在添加
        List<MpmWorkingCalendarEntity> list = this.queryByWhere("calendartype='"+calendartype+"' and periodmonth='"+periodmonth+"'");
        //遍历添加
        Calendar cal = Calendar.getInstance();
        Calendar tempCal = Calendar.getInstance();
        String sDate01=periodmonth.substring(0,4)+"-"+periodmonth.substring(4)+"-01";
        Date dt01=ConvertUtil.convertToDate(sDate01);
        cal.setTime(dt01);
        int month=cal.get(Calendar.MONTH);
        boolean existBool=false;

        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int x=1;x<=lastDay;x++)
        {
            existBool=false;
            for(MpmWorkingCalendarEntity entity:list)
            {
                tempCal.setTime(entity.getSalaryday());
                if(cal.get(Calendar.DAY_OF_MONTH)==tempCal.get(Calendar.DAY_OF_MONTH))
                {
                    existBool=true;
                    break;
                }
            }
            if(existBool)continue;
            CboWorkingCalendarEntity entity=new CboWorkingCalendarEntity();
            entity.setPeriodmonth(ConvertUtil.convertToInt(periodmonth));
            entity.setSalaryday(cal.getTime());
            entity.setCalendartype(calendartype);
            if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)//周日
            {
                entity.setCategory(CboWorkingCalendarCategoryEnum.Weekend);
                entity.setCoefficient(new BigDecimal(CboMultipleOvertimeEnum.M20));
                entity.setDescription("星期天");
            }
            else if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)//周六
            {
                entity.setCategory(CboWorkingCalendarCategoryEnum.Workday);
                entity.setCoefficient(new BigDecimal(CboMultipleOvertimeEnum.M20));
                entity.setDescription("星期六");
            }
            else if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY)//周一
            {
                entity.setCategory(CboWorkingCalendarCategoryEnum.Workday);
                entity.setCoefficient(new BigDecimal(CboMultipleOvertimeEnum.M15));
                entity.setDescription("星期一");
            }
            else if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.TUESDAY)//周二
            {
                entity.setCategory(CboWorkingCalendarCategoryEnum.Workday);
                entity.setCoefficient(new BigDecimal(CboMultipleOvertimeEnum.M15));
                entity.setDescription("星期二");
            }
            else if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.WEDNESDAY)//周三
            {
                entity.setCategory(CboWorkingCalendarCategoryEnum.Workday);
                entity.setCoefficient(new BigDecimal(CboMultipleOvertimeEnum.M15));
                entity.setDescription("星期三");
            }
            else if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.THURSDAY)//周四
            {
                entity.setCategory(CboWorkingCalendarCategoryEnum.Workday);
                entity.setCoefficient(new BigDecimal(CboMultipleOvertimeEnum.M15));
                entity.setDescription("星期四");
            }
            else if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.FRIDAY)//周五
            {
                entity.setCategory(CboWorkingCalendarCategoryEnum.Workday);
                entity.setCoefficient(new BigDecimal(CboMultipleOvertimeEnum.M15));
                entity.setDescription("星期五");
            }
            this.insert(entity);
            cal.add(Calendar.DAY_OF_YEAR, 1);
        }
    }

    /**
     * @author chengengwei
     * @Description 获取n天前的工作日或假期
     * @Date 2021-07-26
     * @Param
     * @return Date
     **/
    @Override
    public Date queryEarlierDateByCategoryAndInterval(String category, Date flagDate, int interval) {
        Map<String, Object> params = new HashMap<>();
        params.put("category", category);
        params.put("salaryday", DateUtil.format(flagDate, "yyyy-MM-dd HH:mm:ss"));
        // +1为参数
        params.put("interval", interval + 1);
        List<MpmWorkingCalendarEntity> queryAgoWorkingDays = this.query(params, "queryAgoWorkingDays");
        if (queryAgoWorkingDays.isEmpty()) {
            return null;
        } else {
            MpmWorkingCalendarEntity mpmWorkingCalendarEntity = queryAgoWorkingDays.get(queryAgoWorkingDays.size() - 1);
            return mpmWorkingCalendarEntity.getSalaryday();
        }
    }
}
